#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: riversystem_info.do
 Author:  Avi Ebenstein
 Created: August 2008
=================================================*/

*******************;

use ~/pollution/GIS/county_points/final_ccw2000_distances.dta, clear;
keep gbcnty frmup_flow toup_flow frmdn_flow todn_flowl strorder frmelevati flowacc pf_type gradient;
sort gbcnty;
save ~/pollution/datafiles/county_closest_stream,replace;
 
************************************************************;
* Bring in the upstream/downstream data of pfafcoded basins ;
************************************************************;
  
insheet using ~/pollution/GIS/Hydro1k/china_hydro1k_pfafcoded.csv,clear;
bysort level6: keep if _n==1;
sort level6;
save ~/pollution/datafiles/hydro1k_pfaf.dta, replace;

*****************************************************************;
* Define the closest water point and river system of each county ;
*****************************************************************;
  
use ~/pollution/GIS/county_points/county_closest_water_2000_nearest3.dta;

capture drop objectid distance;
rename n1_id objectid;
rename n1_cdis distance;

******************************************************************************************;
* I should technically re-assign the counties that were linked to sites with missing data ;
******************************************************************************************;

label var objectid "ID number of closest water monitoring station";
label var distance "Distance to closest water monitoring station";
keep gbcnty eprov ecnty gbcnty gbcode objectid distance level*;

sort gbcnty;
merge gbcnty using ~/pollution/datafiles/county_closest_stream;

keep if _merge==1|_merge==3;
capture drop _merge;

sort level6;
merge level6 using ~/pollution/datafiles/hydro1k_pfaf;

keep if _merge==1|_merge==3;
capture drop _merge;

sort objectid;
merge objectid using ~/pollution/GIS/water_points/waterpoints.dta;
keep if _merge==1|_merge==3;
capture drop _merge;


global waterquality "overall_q permanganate a_n lead oils volatile_phenol
bod mercury dissolved_oxygen";
foreach i of global waterquality{;
                                 rename `i' `i'_point;
                               };

gen CNTYGB=gbcnty;
sort CNTYGB;
save ~/pollution/datafiles/riversystem_info, replace;
